package com.tuanzi.loan.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import com.tuanzi.loan.business.entity.LoginUser;
import com.tuanzi.loan.core.util.IPUtils;
import com.tuanzi.loan.log.entity.LoginLogAction;
import com.tuanzi.loan.log.service.LoginLogService;

@Component
public class LoanLogoutSuccessHandler implements LogoutSuccessHandler {

	@Autowired
	private LoginLogService loginLogService;

	@Override
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
		loginLogService.log(LoginLogAction.LOGOUT, ((LoginUser) authentication.getPrincipal()).getUsername(), IPUtils.getIpAddress(request));
		response.sendRedirect(request.getContextPath() + "/login.xhtml");
	}

}
